package com.jet.goods.gate;

import com.jet.core.result.ResultJson;
import com.jet.core.enums.CommonDeleted;
import com.jet.goods.domain.entity.GoodsCategory;
import com.jet.goods.domain.enums.CategoryLevel;
import com.jet.goods.domain.query.CategoryListQuery;
import com.jet.goods.domain.vo.CategoryTreeVo;
import com.jet.goods.service.IGoodsCategoryService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dcl
 * @date 2023-02-24 13:05:16
 */
@RestController
@RequestMapping("/gate/goods/category")
public class GateCategoryController {

    private final IGoodsCategoryService goodsCategoryService;

    public GateCategoryController(IGoodsCategoryService goodsCategoryService) {
        this.goodsCategoryService = goodsCategoryService;
    }

    @GetMapping("/list")
    public ResultJson<List<GoodsCategory>> list(@RequestHeader long merchantNo,
                                                @RequestHeader long storeNo,
                                                @RequestHeader String accountId) {
        CategoryListQuery query = new CategoryListQuery();
        query.setDeleted(CommonDeleted.NOT_DELETED.getCode());
        List<GoodsCategory> list = goodsCategoryService.findAll(merchantNo, query);
        return ResultJson.ok(list);
    }

    @GetMapping("/tree")
    public ResultJson<List<CategoryTreeVo>> tree(@RequestHeader long merchantNo,
                                                 @RequestHeader long storeNo,
                                                 @RequestHeader String accountId) {
        List<CategoryTreeVo> list = goodsCategoryService.tree(merchantNo);
        return ResultJson.ok(list);
    }

    /**
     * 只返回一级分类
     *
     * @param merchantNo
     * @param storeNo
     * @param accountId
     * @return
     */
    @GetMapping("/one-level/list")
    public ResultJson<List<GoodsCategory>> oneLevelList(@RequestHeader long merchantNo,
                                                        @RequestHeader long storeNo,
                                                        @RequestHeader String accountId) {
        CategoryListQuery query = new CategoryListQuery();
        query.setLevel(CategoryLevel.ONE.getCode());
        query.setDeleted(CommonDeleted.NOT_DELETED.getCode());
        List<GoodsCategory> list = goodsCategoryService.findAll(merchantNo, query);
        return ResultJson.ok(list);
    }

    @GetMapping("/sub-list/{pid}")
    public ResultJson<List<GoodsCategory>> subList(@RequestHeader long merchantNo,
                                                   @RequestHeader long storeNo,
                                                   @RequestHeader String accountId,
                                                   @PathVariable("pid") String pid) {
        CategoryListQuery query = new CategoryListQuery();
        query.setPid(pid);
        query.setDeleted(CommonDeleted.NOT_DELETED.getCode());
        query.setLevel(CategoryLevel.TWO.getCode());
        List<GoodsCategory> list = goodsCategoryService.findAll(merchantNo, query);
        return ResultJson.ok(list);
    }
}
